package org.lucky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lucky.mapper.InviteMapper;
import org.lucky.mapper.LuckyMapper;
import org.lucky.mapper.UserMapper;
import org.lucky.service.IInviteService;
import org.lucky_common.domain.InviteParam;
import org.lucky_common.domain.LuckyShare;
import org.lucky_common.domain.ParseParamObj;
import org.lucky_common.domain.User;
import org.lucky_common.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class InviteServiceImpl extends ServiceImpl<InviteMapper, InviteParam> implements IInviteService {
    @Resource
    private InviteMapper inviteMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private LuckyMapper luckyMapper;
    @Resource
    private UserMapper userMapper;
    @Override
    public AjaxResult saveInvite(InviteParam invite) {
        InviteParam inviteCode = inviteMapper.selectOne(new QueryWrapper<InviteParam>().eq("invitedCode", invite.getInvitedCode()));
        LuckyShare invitedCode = luckyMapper.selectOne(new QueryWrapper<LuckyShare>().eq("inviteCode", invite.getInvitedCode()));
        System.out.println(invitedCode);
        invitedCode.setSecondStage(true);
        luckyMapper.updateById(invitedCode);
        if (inviteCode != null){
            return AjaxResult.error();
        }else {
            inviteMapper.insert(invite);
            return AjaxResult.success("初始化成功");
        }
    }

    @Override
    public AjaxResult addCount(InviteParam invite) {
        InviteParam inviteCode = inviteMapper.selectOne(new QueryWrapper<InviteParam>().eq("invitedCode", invite.getInvitedCode()));
        Boolean aBoolean = redisTemplate.hasKey("share:" + invite.getInvitedCode());
        if (inviteCode != null){
            LuckyShare inviteCode1 = luckyMapper.selectOne(new QueryWrapper<LuckyShare>().eq("inviteCode", inviteCode.getInvitedCode()));
            if (!Boolean.TRUE.equals(aBoolean)){
                redisTemplate.opsForValue().set("share:"+inviteCode.getInvitedCode(),inviteCode1.getUserId().toString(),30, TimeUnit.MINUTES);
            }
            inviteCode.setCount2(inviteCode.getCount2() + 1);
            inviteMapper.updateById(inviteCode);
            return AjaxResult.success("effective");
        }else {
            return AjaxResult.error();
        }
    }

    @Override
    public AjaxResult verifyShare(ParseParamObj parseParamObj) {
        if (parseParamObj.getRef2() != null){
            InviteParam invitedCode = inviteMapper.selectOne(new QueryWrapper<InviteParam>().eq("invitedCode", parseParamObj.getRef2()));
            if (parseParamObj.getRef().equals(parseParamObj.getRef2())) {
                return AjaxResult.success("self");
            }
            Boolean aBoolean = redisTemplate.hasKey("share:"+parseParamObj.getRef2());
            if (Boolean.TRUE.equals(aBoolean)){
                if (invitedCode.getParentId() !=null ){
                    return AjaxResult.success("openDialog");
                }else {
                    return AjaxResult.success("openDialog4");
                }
            }else {
                if (invitedCode != null){
                    LuckyShare inviteCode = luckyMapper.selectOne(new QueryWrapper<LuckyShare>().eq("inviteCode", invitedCode.getInvitedCode()));
                    User id = userMapper.selectOne(new QueryWrapper<User>().eq("id", inviteCode.getUserId()));
                    if (id != null){
                        redisTemplate.expire("share:"+parseParamObj.getRef2(),30,TimeUnit.MINUTES);
                    }

                    if (invitedCode.getParentId() != null ){
                        return AjaxResult.success("openDialog");
                    }else {
                        return AjaxResult.success("openDialog4");
                    }
                }else {
                    return AjaxResult.success("flush");
                }
            }
        }else {
            if (parseParamObj.getRef() != null){
                Boolean aBoolean = redisTemplate.hasKey("share:" + parseParamObj.getRef());
                LuckyShare inviteCode = luckyMapper.selectOne(new QueryWrapper<LuckyShare>().eq("inviteCode", parseParamObj.getRef()));
                if (Boolean.TRUE.equals(aBoolean) && inviteCode !=null ){
                    return AjaxResult.success("flush");
                }else {
                    return AjaxResult.success("valid");
                }
            }else {
                return AjaxResult.success("valid");
            }
        }
    }

    @Override
    public AjaxResult addEffectiveCount(ParseParamObj parseParamObj) {
        String s = redisTemplate.opsForValue().get("share:" + parseParamObj.getRef());
        System.out.println(s);
        if (Boolean.TRUE.equals(redisTemplate.hasKey("share:" + parseParamObj.getRef()))) {
            InviteParam invitedCode = inviteMapper.selectOne(new QueryWrapper<InviteParam>().eq("invitedCode", parseParamObj.getRef()));
            if (invitedCode.getParentId() != null){
                return AjaxResult.success("void");
            }else {
                if (parseParamObj.getRef2() != null){
                    InviteParam inviteCode2 = inviteMapper.selectOne(new QueryWrapper<InviteParam>().eq("invitedCode", parseParamObj.getRef2()));
                    inviteCode2.setParentId(invitedCode.getId());
                    inviteMapper.updateById(inviteCode2);
                }
                invitedCode.setEffectiveCount(invitedCode.getEffectiveCount()+1);
                inviteMapper.updateById(invitedCode);
                return AjaxResult.success("effective");
            }
        }else {
            InviteParam inviteCode = inviteMapper.selectOne(new QueryWrapper<InviteParam>().eq("invitedCode", parseParamObj.getRef()));
            if (inviteCode != null){
                LuckyShare inviteCode2 = luckyMapper.selectOne(new QueryWrapper<LuckyShare>().eq("inviteCode", inviteCode.getInvitedCode()));
                User id = userMapper.selectOne(new QueryWrapper<User>().eq("id", inviteCode2.getUserId()));
                if (id != null){
                    redisTemplate.expire("share:" + parseParamObj.getRef(),30,TimeUnit.MINUTES);
                }
                if (inviteCode.getParentId() != null){
                    return AjaxResult.success("void");
                }else {
                    if (parseParamObj.getRef2() != null){
                        InviteParam inviteCode1 = inviteMapper.selectOne(new QueryWrapper<InviteParam>().eq("inviteCode", parseParamObj.getRef2()));
                        inviteCode1.setParentId(inviteCode.getId());
                        inviteMapper.updateById(inviteCode1);
                    }
                    inviteCode.setEffectiveCount(inviteCode.getEffectiveCount()+1);
                    inviteMapper.updateById(inviteCode);
                    return AjaxResult.success("effective");
                }
            }else {
                return AjaxResult.success("void");
            }
        }
    }

}
